﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SpaceGame.Player;
using SpaceGame.Particles;
using SpaceGame.Managers;

namespace SpaceGame.Enemies
{
    public class BaseEnemy
    {
        #region Private Fields

        private PlayerShip m_Player;
        private Vector2 m_Location;
        private int m_Level;
        private Texture2D m_Texture;
        private float m_Speed = 1.5f;
        private float m_Rotation;
        private float m_Scale;
        private int m_Point = 25;

        #endregion

        #region Attributes

        public int Point
        {
            get { return m_Point; }
            set { m_Point = value; }
        }

        public float Scale
        {
            get { return m_Scale; }
            set { m_Scale = value; }
        }

        public Vector2 Location
        {
            get { return m_Location; }
            set { m_Location = value; }
        }

        public Texture2D Texture
        {
            get { return m_Texture; }
            set { m_Texture = value; }
        }

        public float Speed
        {
            get { return m_Speed; }
            set { m_Speed = value; }
        }

        public float Rotation
        {
            get { return m_Rotation; }
            set { m_Rotation = value; }
        }

        public int Level
        {
            get { return m_Level; }
            set { m_Level = value; }
        }

        #endregion

        public BaseEnemy(PlayerShip player)
        {
            m_Player = player;
        }

        public void TurnTo(Vector2 turnTo)
        {
            Vector2 m_Direction = turnTo - m_Location;
            double dNewRotation = 0;
            float fDirection = 0.5f;
            if (m_Direction.X != 0 && m_Direction.Y != 0)
                dNewRotation = Math.Atan2(m_Direction.Y / m_Direction.X, turnTo.Y / turnTo.X);
            else if (m_Direction.X == 0 && m_Direction.Y > 0)
                fDirection = 0.0f;
            if (m_Direction.X < 0)
                dNewRotation -= (Math.PI * fDirection);
            else
                dNewRotation += (Math.PI * fDirection);
            if (dNewRotation < 0)
                dNewRotation += (Math.PI * 2);
            m_Rotation = (float)dNewRotation - 90.0f;
        } 

        public void Update(GameTime gameTime)
        {
            if (GameStateManager.GameState != GameState.Playing)
                return;

            TurnTo(m_Player.Location);

            double elapsed = gameTime.ElapsedGameTime.TotalMilliseconds;

            Vector2 normalizedLocation = m_Player.Location - m_Location;
            normalizedLocation.Normalize();

            m_Location += normalizedLocation * m_Speed;

            Rectangle playerRectangle = new Rectangle((int)m_Player.Location.X, (int)m_Player.Location.Y, (int)m_Player.ShipSize.X, (int)m_Player.ShipSize.Y);
            Rectangle enemyRectangle = new Rectangle((int)m_Location.X, (int)m_Location.Y, (int)m_Texture.Width, (int)m_Texture.Height);

            if (playerRectangle.Intersects(enemyRectangle))
                m_Player.Destroy(this);
        }

        public void Destroy()
        {
            ExplosionParticleSystem explosion = new ExplosionParticleSystem(m_Player.Game, 1);
            m_Player.Game.Components.Add(explosion);
            explosion.AddParticles(m_Location);
            m_Player.Enemies.Remove(this);

            /*for (int i = 0; i < m_Player.Game.Components.Count; i++)
                if (m_Player.Game.Components[i] is ExplosionParticleSystem)
                    if (((ExplosionParticleSystem)m_Player.Game.Components[i]).FreeParticleCount <= 0)
                        m_Player.Game.Components.RemoveAt(i);
             */
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (GameStateManager.GameState != GameState.Playing)
                return;

            spriteBatch.Draw(m_Texture, m_Location, null, Color.Red, m_Rotation,
                new Vector2(m_Texture.Width / 2, m_Texture.Height / 2), m_Scale, SpriteEffects.None,
                1.0f);
        }
    }
}
